import { Injectable, NotFoundException } from '@nestjs/common'
import { ArticleCreateDTO } from './dto/article-create.dto'
import { ArticleEditDTO } from './dto/article-edit.dto'
import { IdDTO } from './dto/id.dto'
import { ListDTO } from './dto/list.dto'
import { InjectRepository } from '@nestjs/typeorm'
import { Article } from './entity/article.entity'
import { Repository } from 'typeorm'
import { getPagination } from 'src/utils/util'

@Injectable()
export class ArticleService {
  list: any[] // 临时数据
  constructor(
    @InjectRepository(Article)
    private readonly articleRepository: Repository<Article>,
  ) {
    this.list = []
  }

  // 获取列表
  async getList(listDTO: ListDTO) {
    const { page = 1, pageSize = 10 } = listDTO
    const getList = this.articleRepository
      .createQueryBuilder('article')
      .where({ isDelete: false })
      .select([
        'article.id',
        'article.title',
        'article.description',
        'article.createTime',
        'article.updateTime',
      ])
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getManyAndCount()
    const [list, total] = await getList
    const pagination = getPagination(total, pageSize, page)

    return {
      list,
      pagination,
    }
  }

  // 获取详情
  async getInfo(idDTO: IdDTO) {
    const { id } = idDTO
    const articleDetail = await this.articleRepository
      .createQueryBuilder('article')
      .where('article.id = :id', { id })
      .getOne()

    if (!articleDetail) {
      throw new NotFoundException('找不到文章！')
    }

    return {
      info: articleDetail,
    }
  }

  // 创建文章
  async created(articleCreateDTO: ArticleCreateDTO) {
    const article = new Article()
    article.title = articleCreateDTO.title
    article.description = articleCreateDTO.description
    article.content = articleCreateDTO.content
    const result = await this.articleRepository.save(article)

    return {
      info: result,
    }
  }

  // 更新文章
  async update(articleEditDTO: ArticleEditDTO) {
    const { id } = articleEditDTO
    const articleToUpdate = await this.articleRepository.findOneById(id)
    articleToUpdate.title = articleEditDTO.title
    articleToUpdate.description = articleEditDTO.description
    articleToUpdate.content = articleEditDTO.content
    const result = await this.articleRepository.save(articleToUpdate)

    return {
      info: result,
    }
  }

  // 删除文章
  async delete(idDTO: IdDTO) {
    const { id } = idDTO
    const articleToUpdate = await this.articleRepository.findOneById(id)
    articleToUpdate.isDelete = true
    const result = await this.articleRepository.save(articleToUpdate)
    return {
      info: result,
    }
  }
}
